package com.bqtv.sport.ui.im

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bqtv.sport.R
import com.bqtv.sport.adapter.RecommendFriendAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.databinding.ActivityMyNewFriendBinding
import com.bqtv.sport.ui.mine.QRCodeScanActivity
import com.tencent.qcloud.tuikit.timcommon.component.interfaces.IUIKitCallback
import com.tencent.qcloud.tuikit.tuicontact.bean.FriendApplicationBean
import com.tencent.qcloud.tuikit.tuicontact.classicui.widget.NewFriendListAdapter
import com.tencent.qcloud.tuikit.tuicontact.interfaces.INewFriendActivity
import com.tencent.qcloud.tuikit.tuicontact.presenter.NewFriendPresenter
import com.tencent.qcloud.tuikit.tuicontact.util.TUIContactLog

class MyNewFriendActivity : AppActivity<ActivityMyNewFriendBinding>(), INewFriendActivity {
    private lateinit var presenter: NewFriendPresenter
    private lateinit var mAdapter: NewFriendListAdapter
    private lateinit var recommendAdapter: RecommendFriendAdapter

    companion object {
        private val TAG: String = MyNewFriendActivity::class.java.getSimpleName()
        fun start(context: Context) {
            val intent = Intent(context, MyNewFriendActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getViewBinding() = ActivityMyNewFriendBinding.inflate(layoutInflater)

    override fun initView() {
        binding.rcvRecommend.layoutManager = LinearLayoutManager(this)
        recommendAdapter = RecommendFriendAdapter()
        binding.rcvRecommend.adapter = recommendAdapter
    }

    override fun initData() {
        presenter = NewFriendPresenter()
        presenter.setFriendActivity(this)
        presenter.setFriendApplicationRead(object : IUIKitCallback<Void?>() {
            override fun onSuccess(data: Void?) {}

            override fun onError(module: String?, errCode: Int, errMsg: String?) {}
        })

        initListener()
    }

    override fun onResume() {
        super.onResume()
        initPendency()
    }

    private fun initPendency() {
        presenter.loadFriendApplicationList()
    }

    private fun initListener() {
        binding.ivScan.setOnClickListener {
            QRCodeScanActivity.start(this, object : QRCodeScanActivity.OnScanListener {
                override fun onSucceed(content: String?) {
                }
            })
        }
        binding.rlSearch.setOnClickListener {
            SearchFriendActivity.start( this)
        }
        recommendAdapter.setOnItemClickListener { _, _, _ ->
            FriendHomeActivity.start(this,"")
        }
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {

        }
    }

    override fun onDataSourceChanged(dataSource: List<FriendApplicationBean?>?) {
        TUIContactLog.i(TAG, "getFriendApplicationList success")
        binding.newFriendList.setVisibility(View.VISIBLE)
        mAdapter = NewFriendListAdapter(
            this,
            R.layout.contact_new_friend_item,
            dataSource
        )
        mAdapter.setPresenter(presenter)
        binding.newFriendList.setAdapter(mAdapter)
        mAdapter.notifyDataSetChanged()
    }
}